package com.mall.b2c.controller;

import static com.mall.b2c.Constants.CART_COOKIE_KEY;
import static com.mall.b2c.Constants.CART_COOKIE_QUANTITY;
import static com.mall.b2c.Constants.PRINCIPAL_SESSION_NAME;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mall.b2c.bean.CartBean;
import com.mall.b2c.bean.GoodsBean;
import com.mall.b2c.bean.Principal;
import com.mall.b2c.hessian.TradeHessian;
import com.mall.common.bean.Message;
import com.mall.common.util.AppContext;
import com.mall.common.util.CookieUtils;
import com.mall.common.validation.annotation.IntRangeFieldValidator;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.Validations;

@Controller
@RequestMapping("/cart")
public class CartController extends BaseController {
	@Resource private TradeHessian tradeHessian;

	/**
	 * 购物车列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model model, HttpServletRequest request, HttpServletResponse response){
		CartBean cart = jsonToObj(tradeHessian.getCartItemInfo(getMemberId(), null, true), CartBean.class);
		model.addAttribute("cart", cart);
		model.addAttribute("favoriteList",jsonToList(memberHessian.findFavoriteList(getMemberId(), 10), GoodsBean.class));
		CookieUtils.addGlobalCookie(request, response, CART_COOKIE_QUANTITY, cart.getItemNum().toString(), null);
		return "/cart";
	}
	
	/**
	 * 获取购物车信息
	 * @return
	 */
	@RequestMapping("/getCartItemInfo")
	@ResponseBody
	public CartBean getCartItemInfo(){
		Principal principal = (Principal) AppContext.getSessionAttr(PRINCIPAL_SESSION_NAME);
		if(principal != null){
			return jsonToObj(tradeHessian.getCartItemInfo(getMemberId(), null, true), CartBean.class);
		}
		return null;
	}
	
	/**
	 * 加入购物车
	 * @param goodsId
	 * @param num
	 * @return
	 */
	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="goodsId", message="商品不存在"),
					@RequiredFieldValidator(fieldName="num", message="购买数量不正确")
			},
			intRangeFields={
					@IntRangeFieldValidator(fieldName="num", min="1", max="10000", message="购买数量不正确")
			}
	)
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Message add(Long goodsId, Integer num, HttpServletRequest request, HttpServletResponse response){
		String key = CookieUtils.get(request, CART_COOKIE_KEY);
		JSONObject result = JSONObject.parseObject(tradeHessian.addCartItem(getMemberId(), key, goodsId, num));
		if(result.containsKey("cartKey")){
			CookieUtils.addGlobalCookie(request, response, CART_COOKIE_KEY, result.getString("cartKey"), null);
		}
		CookieUtils.addGlobalCookie(request, response, CART_COOKIE_QUANTITY, result.getString("itemNum"), null);
		return successMessage;
	}

	/**
	 * 批量加入购物车，数量为1
	 * @param goodsIds 商品id
	 * @param request
	 * @param response
	 * @return
	 */
	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="goodsIds", message="商品不存在")
			}
	)
	@RequestMapping(value="/batchAdd",method=RequestMethod.POST)
	@ResponseBody
	public Message batchAdd(Long[] goodsIds, HttpServletRequest request, HttpServletResponse response){
		JSONObject result = JSONObject.parseObject(tradeHessian.batchAddCartItem(getMemberId(), goodsIds));
		CookieUtils.addGlobalCookie(request, response, CART_COOKIE_QUANTITY, result.getString("itemNum"), null);
		return successMessage;
	}

	/**
	 * 移出购物车
	 * @param itemIds 购物车项id
	 * @return
	 */
	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="itemIds", message="商品不存在")
			}
	)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Message delete(Long[] itemIds, HttpServletRequest request, HttpServletResponse response){
		JSONObject result = JSONObject.parseObject(tradeHessian.deleteCartItems(getMemberId(), itemIds));
		CookieUtils.addGlobalCookie(request, response, CART_COOKIE_QUANTITY, result.getString("itemNum"), null);
		return successMessage;
	}

	/**
	 * 修改购物车项数量
	 * @param itemId 购物车项id
	 * @param quantity 数量
	 * @return
	 */
	@Validations(
		requiredFields = {
				@RequiredFieldValidator(fieldName="itemId", message="购买商品不存在"),
				@RequiredFieldValidator(fieldName="quantity", message="数量不能为空")
		},
		intRangeFields={
				@IntRangeFieldValidator(fieldName="quantity", min="1", max="10000", message="购买数量不正确")
		}
	)
	@RequestMapping(value="/modifyNum",method=RequestMethod.POST)
	@ResponseBody
	public Message modifyNum(Long itemId, Integer quantity){
		tradeHessian.modifyCartItem(getMemberId(), itemId, quantity);
		return successMessage;
	}





	/**
	 * 加入积分购物车
	 * @param goodsId
	 * @param num
	 * @return
	 */
	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="id", message="商品不存在"),
					@RequiredFieldValidator(fieldName="num", message="购买数量不正确")
			},
			intRangeFields={
					@IntRangeFieldValidator(fieldName="num", min="1", max="10000", message="购买数量不正确")
			}
	)
	@RequestMapping(value="/add2",method=RequestMethod.POST)
	@ResponseBody
	public Message add2(Long id, Integer num, HttpServletRequest request, HttpServletResponse response){
		String key = CookieUtils.get(request, CART_COOKIE_KEY);
		JSONObject result = JSONObject.parseObject(tradeHessian.addCartItem2(getMemberId(), key, id, num));
		if(result.containsKey("cartKey")){
			CookieUtils.addGlobalCookie(request, response, CART_COOKIE_KEY, result.getString("cartKey"), null);
		}
		CookieUtils.addGlobalCookie(request, response, CART_COOKIE_QUANTITY, result.getString("itemNum"), null);
		return successMessage;
	}

	/**
	 * 移出积分购物车
	 * @param itemIds 购物车项id
	 * @return
	 */
	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="itemIds", message="商品不存在")
			}
	)
	@RequestMapping(value="/delete2",method=RequestMethod.POST)
	@ResponseBody
	public Message delete2(Long[] itemIds, HttpServletRequest request, HttpServletResponse response){
		JSONObject result = JSONObject.parseObject(tradeHessian.deleteCartItems2(getMemberId(), itemIds));
		CookieUtils.addGlobalCookie(request, response, CART_COOKIE_QUANTITY, result.getString("itemNum"), null);
		return successMessage;
	}

	/**
	 * 修改积分购物车项数量
	 * @param itemId 购物车项id
	 * @param quantity 数量
	 * @return
	 */
	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="itemId", message="购买商品不存在"),
					@RequiredFieldValidator(fieldName="quantity", message="数量不能为空")
			},
			intRangeFields={
					@IntRangeFieldValidator(fieldName="quantity", min="1", max="10000", message="购买数量不正确")
			}
	)
	@RequestMapping(value="/modifyNum2",method=RequestMethod.POST)
	@ResponseBody
	public Message modifyNum2(Long itemId, Integer quantity){
		tradeHessian.modifyCartItem2(getMemberId(), itemId, quantity);
		return successMessage;
	}


}
